﻿using System;
using System.Collections.Generic;
using System.Linq;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsprintbilltypeServices : Repository<corecmsprintbilltype>, IcorecmsprintbilltypeServices
    {
        public bool InsertAsync(corecmsprintbilltype entity)
        {
            return base.Change<corecmsprintbilltype>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsprintbilltype> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsprintbilltype entity)
        {
            return base.Change<corecmsprintbilltype>().Update(a => new corecmsprintbilltype()
            {
                name = entity.name,
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsprintbilltype> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsprintbilltype>().Update(a => new corecmsprintbilltype()
                {
                    name = item.name,
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsprintbilltype>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsprintbilltype>().DeleteById(id);
            }
            return returnKey;
        }

        #region 获取缓存的所有数据==========================================================
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsprintbilltype> GetCaChe()
        {
            return base.GetList();
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintbilltype> QueryAsync(long typeId)
        {
            return base.AsQueryable().Where(p => p.id > 0).OrderBy(a => a.id).ToList();
        }
        #endregion

        /// <summary>
        /// 根据id查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public corecmsprintbilltype SelectByType(int type)
        {
            return base.Change<corecmsprintbilltype>().GetSingle(a => a.id == type);
        }
    }
}